﻿Public Partial Class GaleriaDeFotos1
    Inherits System.Web.UI.Page
    Protected script As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            EncheGrid()
            If Not String.IsNullOrEmpty(Me.Request.QueryString("IDAlbum")) Then
                Me.txtIDAlbum.Text = Me.Request.QueryString("IDAlbum").ToString
                mostraregistro()
                tbAlbum.Visible = False
                tbFotos.Visible = True
                MostraFotos()
            End If
        End If
    End Sub
    Private Sub EncheGrid()
        Dim fun As New Funcoes
        Dim c As New SiteDLL.Album
        Try
            datalistAlbum.DataSource = c.executarQuerySQL(" SELECT " & _
                                                          "   Album.IDAlbum, " & _
                                                          "   (Select TOP 1 Fotos.Link FROM Fotos WHERE Fotos.IDAlbum = Album.IDAlbum) as Link, " & _
                                                          "   Album.Nome " & _
                                                          " FROM Album " & _
                                                          " ORDER BY IDAlbum DESC ")
            datalistAlbum.DataBind()
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub
    Private Sub mostraregistro()
        Dim fun As New Funcoes
        Dim c As New SiteDLL.Album
        Try
            c.IDAlbum = Val(txtIDAlbum.Text)
            c.carregar()
            lblAlbum.Text = c.Nome ' na tabela envia fotos
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Protected Sub datalistAlbum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles datalistAlbum.SelectedIndexChanged
        Dim lbl As Label
        lbl = datalistAlbum.Items(datalistAlbum.SelectedIndex).FindControl("lblID")
        txtIDAlbum.Text = lbl.Text
        mostraregistro()
        tbAlbum.Visible = False
        tbFotos.Visible = True
        MostraFotos()
    End Sub
    Private Sub MostraFotos()
        Dim fun As New Funcoes
        Dim c As New SiteDLL.Fotos
        Try
            c.addWhere("IDAlbum", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, Val(txtIDAlbum.Text))
            DataListFotos.DataSource = c.getTodosRegistros
            DataListFotos.DataBind()

        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Protected Sub btnLimpar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpar.Click
        Response.Redirect(Request.Path)

    End Sub

    Protected Sub DataListFotos_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataListFotos.SelectedIndexChanged
        Dim img As ImageButton
        Dim fun As New Funcoes
        img = DataListFotos.Items(DataListFotos.SelectedIndex).FindControl("ImageButton2")
        script += fun.AbreRelatorio(img.ImageUrl, "Imagem")
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "msg", script, False)
        fun = Nothing

    End Sub
End Class